var AssetAdd = function () {
	var handleValidation = function() {
		 return {
	            init: function () {
			//表格初始化
	        var oTable = $('#assetAllocationDetailEditable').dataTable({
	        	"paginate": false, //翻页功能
                "autoWidth":false,
                "displayLength": 10,
                "info":false,
                "lengthChange":false,
                "language": {
                    "zeroRecords": "数据为空"
                },
	            "columnDefs": [
					{
					    "data": null,
					    'sortable': false,
					    "defaultContent": "Edit",
					    "targets": [9]
					}            
	            ],
	            "columns": [
	               { "data": "orders" ,'sortable': false,"defaultContent": "" },
	               { "data": "projectName" ,'sortable': false,"defaultContent": "" },
	               { "data": "projectAmount" ,'sortable': false,"defaultContent": "" },
	               { "data": "amountInvested" ,'sortable': false,"defaultContent": "" },
	               { "data": "bankName" ,'sortable': false,"defaultContent": "" },
	               { "data": "bankDeposit" ,'sortable': false,"defaultContent": "" },
	               { "data": "bankAccount" ,'sortable': false,"defaultContent": "" },
	               { "data": "amount" ,'sortable': false,"defaultContent": "" },
	               { "data": "remark" ,'sortable': false,"defaultContent": "" },
	           ],
	           "filter" : false,
	           "processing": false,
	           "serverSide": true,
	           "ajaxSource": ctx + "/assetAllocation/query",
	           "serverMethod": "post",
	           "fnRowCallback" : function(nRow, aData, displayIndex) {
	        	   nRow.children[0].id = aData.id;
	        	   var operation = "";
	        	   operation = '<a class="edit fa fa-pencil-square-o fa-lg pull-left" title="编辑" href="javascript:;"></a>\
	        		   &nbsp;<a title="删除" class="delete fa fa-trash-o fa-lg pull-left" href="javascript:;"></a>';
	        	   $('td:eq(0)', nRow).html(++displayIndex);
	        	   $('td:eq(9)', nRow).html(operation);
	               return nRow;
	           },
	           "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	               var json = "";
	               var s;
	               for (var i = 0; i < dataSet.length; i++) {
	                   s = dataSet[i];
	               	   if (s.name == 'sEcho') {
	               		   json = json + '"echo":' + s.value;
	                   } else if (s.name == 'iDisplayStart') {
	                	   json = json + ',"start":' + s.value;
	                   } else if (s.name == 'iDisplayLength') {
	                	   json = json + ',"pageSize":' + s.value;
	                   }
	               }
	               var obj= {};
	               var assetAllocationId = $('#assetAllocationId').val();
	               obj.assetAllocationId = assetAllocationId;
	               var json = JSON.stringify(obj); 
	               $.ajax({
	               	"contentType":"application/json; charset=UTF-8",
	                   "dataType" : 'json',
	                   "type" : "POST",
	                   "url" : ajaxSource,
	                   "data" : json,
	                   "success" : fnCallback
	               });
	           },
	        });
        
	        //编辑
            $('#assetAllocationDetailEditable').on("click", "a.edit", function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
                var id = nRow.children[0].id;
                $.ajax({
                    type: "post",
                    url: ctx + "/assetAllocation/edit/"+id,
                    dataType: "json",
                    success: function(msg) {
                        if (msg.code == 1) {
                        	$("#editBody").empty();// clean
                        	var tmpl = $("#editTemplate").tmpl(msg.data).appendTo("#editBody");
                        	var modal = $('#editModal').modal('show');
                            var editDetailForm = $('#editDetailForm');
                            modal.on("hidden.bs.modal", function(e) {
                                $(this).removeData();
                            });
                            
                            editDetailForm.validate({
                                focusInvalid: true,
                                rules: {
                                	projectId: {
                                        required: true,
                                    },
                                    amount: {
                                        required: true,
                                        number:true,
                                    },
                                    bankName: {
                                        required: true,
                                    },
                                    bankDeposit: {
                                        required: true,
                                    },
                                    bankAccount: {
                                        required: true,
                                        number:true,
                                        rangelength: [6, 18]
                                    },
                                    remark: {
                                        required: true,
                                    },
                                },
                                messages: {
                                    bankAccount: {
                                        required: "账号不能为空！",
                                    },
                                },
                                success: function(label, element) {
                                    var icon = $(element).parent('.input-icon').children('i');
                                    $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
                                    icon.removeClass("fa-warning").addClass("fa-check");
                                },
                                submitHandler: function(form) {
                                    var form = $("form#editDetailForm").serializeJSON();
                                    var json = form; 
                                    $('#editConfirm').attr('disabled', true);
                                    $.ajax({
                                        type: "post",
                                        contentType: "application/json; charset=UTF-8",
                                        url: ctx + "/assetAllocation/doEditDetail",
                                        data: json,
                                        dataType: "json",
                                        success: function(msg) {
                                            if (msg.code == 1) {
                                                bootbox.alert("修改成功！");
                                                modal.modal('hide');
                                                oTable.fnDraw(false);
                                            } else {
                                                bootbox.alert(msg.message);
                                            }
                                            $('#editConfirm').attr('disabled', false);
                                        },
                                        error: function(msg) {
                                            bootbox.alert(msg.message);
                                            modal.modal('hide');
                                            $('#editConfirm').attr('disabled', false);
                                        }
                                    });
                                }
                            });
                            
                            $('#editConfirm').unbind("click").click(function() {
                                timeout(this, 1);
                                editDetailForm.submit();
                            });
                        } else {
                            bootbox.alert(msg.message);
                        }
                    },
                    error: function(msg) {
                        bootbox.alert(msg.message);
                        modal.modal('hide');
                    }
                });
            });
	        
            
            //删除资产配置
            $('#assetAllocationDetailEditable').on("click", "a.delete", function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
                var id = nRow.children[0].id;
                bootbox.confirm("您确定删除这条记录?", function(result) {
                    if (result) {
                    	 $.ajax({
                             type: "post",
                             url: ctx + "/assetAllocation/delete",
                             data:"id="+id,
                             dataType: "json",
                             success: function(msg) {
                                 if (msg.code == 1) {
                                     bootbox.alert("删除成功！");
                                     oTable.fnDraw(false);
                                 } else {
                                     bootbox.alert(msg.message);
                                 }
                             },
                             error: function(msg) {
                                 bootbox.alert(msg.message);
                                 modal.modal('hide');
                             }
                         });
                    }
                });
            });
            
            //添加资产配置
            $('#assetAllocation').on("click", (function (e) {
	            e.preventDefault();
	            var obj= {};
	            var name = $('#name').val();
	            var productId = $('#productId').val();
	            obj.name = name;
	            obj.productId = productId;
	            var json = JSON.stringify(obj); 
	            $.ajax({
	            	contentType:"application/json; charset=UTF-8",
	            	type:"post",
                    url: ctx + "/assetAllocation/doAdd",
                    data:json,
                    dataType:"json",
                    success: function(msg){
                    	if (msg.code == 1) {
                    		$('#assetAllocation').css('display','none');
                    		$('#assetAllocationDetailRow').css('display','block');
                    		$('#submitAsset').css('display','block');
                    		$('#assetAllocationId').val(msg.data.id);
                    		oTable.fnDraw(false);
                    		//
                    	} else {
                    		bootbox.alert(msg.message);
                    	}
                    },
                    error: function (msg) {
                    	bootbox.alert(msg.message);
                    }
	            });
            }));
            
            //添加资产配置明细
            $('#assetAllocationDetailAdd').click(function() {
                var modal = $('#addModal').modal('show');
                var addDetailForm = $('#addDetailForm');
                modal.on("hidden.bs.modal", function(e) {
                    $(this).removeData();
                    $("#addDetailForm")[0].reset();
                });
               
                addDetailForm.validate({
                    focusInvalid: true,
                    rules: {
                    	projectId: {
                            required: true,
                        },
                        amount: {
                            required: true,
                            number:true,
                        },
                        bankName: {
                            required: true,
                        },
                        bankDeposit: {
                            required: true,
                        },
                        bankAccount: {
                            required: true,
                            number:true,
                            rangelength: [6, 18]
                        },
                        remark: {
                            required: true,
                        },
                    },
                    messages: {
                        bankAccount: {
                            required: "账号不能为空！",
                        },
                    },
                    success: function(label, element) {
                        var icon = $(element).parent('.input-icon').children('i');
                        $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
                        icon.removeClass("fa-warning").addClass("fa-check");
                    },
                    submitHandler: function(form) {
                        var form = $("form#addDetailForm").serializeJSON();
                        var assetAllocationId = $('#assetAllocationId').val();
                        if (assetAllocationId == '') {
                        	bootbox.alert("请先添加资产配资");
                        	return;
                        }
                        var js = form.replace('}', ',');
                        var json = js + '"assetAllocationId" : "' + assetAllocationId + '"}'; 
                        $('#addConfirm').attr('disabled', true);
                        $.ajax({
                            type: "post",
                            contentType: "application/json; charset=UTF-8",
                            url: ctx + "/assetAllocation/doAddDetail",
                            data: json,
                            dataType: "json",
                            success: function(msg) {
                                if (msg.code == 1) {
                                    bootbox.alert("新增成功！");
                                    modal.modal('hide');
                                    oTable.fnDraw(false);
                                } else {
                                    bootbox.alert(msg.message);
                                }
                                $('#addConfirm').attr('disabled', false);
                            },
                            error: function(msg) {
                                bootbox.alert(msg.message);
                                modal.modal('hide');
                                $('#addConfirm').attr('disabled', false);
                            }
                        });
                    }
                });

                $('#addConfirm').unbind("click").click(function() {
                    timeout(this, 1);
                    addDetailForm.submit();
                    $("#addDetailForm")[0].reset();
                });
            
            });
            
            $('#submitAsset').unbind("click").click(function() {
            	var assetAllocationId = $('#assetAllocationId').val();
                if (assetAllocationId == '') {
                	bootbox.alert("请先添加资产配资");
                	return;
                }
                bootbox.confirm("您确定提交这条此次资产配置?", function(result) {
                    if (result) {
                    	$.ajax({
                        	type: "post",
                            url: ctx + "/assetAllocation/submitAudit",
                            data: "id=" + assetAllocationId ,
                            dataType: "json",
                            success: function(msg) {
                                if (msg.code == 1) {
                                	bootbox.confirm("提交审核成功！", function(result) {
                                        if (result) {
                                        	window.location.href = ctx+'/assetAllocation/list';
                                    	} else {
                                    		window.location.href = ctx+'/assetAllocation/list';
                                    	}
                                    });
                                } else {
                                    bootbox.alert(msg.message);
                                }
                            },
                            error: function(msg) {
                                bootbox.alert(msg.message);
                            }
                        });
                    }
                });
            });
            
	            }

	        };    
	}();
	
    return {
        init: function () {
            handleValidation.init();
        }
    };
}();
